const express = require('express')
const morgan = require('morgan')
const cors = require('cors')
const errorHandler = require('./middlewares/errorHandler')
const app = express()
// 默认回去找routes文件夹下面的index.js。注意，只有它下面的文件是index.js的时候才能这样用
const router = require('./routes')
const port = 3000

app.use(cors())


// app.all('*', function(req, res, next) {
//   res.header("Access-Control-Allow-Origin", "*");
//   res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
//   res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
//   if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
//   else next();
// });

// 设置静态文件路径
app.use(express.static('views'));
app.use('/uploads', express.static('uploads'));

// 使用日志中间件
app.use(morgan('short'))

app.use(router)
// 后期给路由添加统一的版本号
// app.use('/v1', router)

// 错误处理中间件
app.use(errorHandler);
app.listen(port, () => console.log(`Example app listening on port ${port}!`))